﻿/*----------------------------------------------------------------
//Copyright (C) 2011 赵国伟
//文件名：SwfUploadHelper.js
//文件功能描述：swfupload辅助。
//创建人：赵国伟
//创建时间：2011-10-30 16:27:00
//----------------------------------------------------------------*/

///创建指定的SwfUpload对象；by 赵国伟
function CreateSwfUploadObject(uploadUrl, sessionID, fileType, fileDescription, uploadButtonSpan, uploadedShowSpan) {

    var swfu = new SWFUpload({
        // Backend Settings
        upload_url: uploadUrl,
        post_params: {
            "ASPSESSID": sessionID
        },

        // File Upload Settings
        file_size_limit: "10 MB",
        file_types: fileType,
        file_upload_limit: "0",    // Zero means unlimited
        file_types_description: fileDescription,
        file_queue_error_handler: fileQueueError,
        file_dialog_complete_handler: fileDialogComplete,
        upload_error_handler: uploadError,
        upload_success_handler: uploadSuccess,
        upload_start_handler: uploadStart,

        prevent_swf_caching: false,
        button_image_url: "/JsBase/swfupload/Upload.jpg?&ttt=" + Math.random(),
        button_placeholder_id: uploadButtonSpan,
        button_width: 160,
        button_height: 22,
        button_text: '<span class="button">请选择文件 <span class="buttonSmall">(10 MB Max)</span></span>',
        button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
        button_text_top_padding: 1,
        button_text_left_padding: 5,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
        // Flash Settings
        flash_url: "/JsBase/swfupload/swfupload.swf?&ttt=" + Math.random(), // Relative to this file

        custom_settings: {
            upload_target: uploadedShowSpan
        },

        // Debug Settings
        debug: false
    });

    return swfu;
}

function uploadStart() {
    $(this.customSettings.upload_target).innerHTML = "<font color=red>文件上传中......</font>";
}

///上传成功后的处理；
function uploadSuccess(file, fileUrl) {
    try {
       //往外赋值；
        $(this.customSettings.upload_target).innerHTML = GetUploadedString(fileUrl);

    } catch (ex) {
        this.debug(ex);
    }
}





function fileQueueError(file, errorCode, message) {
    try {
        var errorName = "";
        if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {
            alert("提示：您没有被允许上传多个文件！");
        }
        if (errorName !== "") {
            alert(errorName);
            return;
        }
        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                alert("提示：上传的文件不能为0字节！");
                break;
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                alert("提示：上传的文件大小超过了系统限制！");
                break;
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
            default:
                alert(message);
                break;
        }
    } catch (ex) {
        this.debug(ex);
    }

}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
    try {
        if (numFilesQueued > 0) {
            this.startUpload();
        }
    } catch (ex) {
        this.debug(ex);
    }
}

function uploadError(file, errorCode, message) {

    var progress;
    try {
        switch (errorCode) {
            case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                try {
                    alert("提示：上传错误已取消！");
                }
                catch (ex1) {
                    this.debug(ex1);
                }
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
                try {
                    alert("提示：上传错误已停止！");
                }
                catch (ex2) {
                    this.debug(ex2);
                }
            case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                alert("提示：上传不被允许");
                break;
            default:
                alert(message);
                break;
        }



    } catch (ex3) {
        this.debug(ex3);
    }

}